package service;

import mapper.Brandmapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import pojo.Brand;
import util.SqlSessionFactoryUtils;

import java.util.List;

public class BrandService {
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();
    public List<Brand> selectAll()
    {
        //在里面调用select的方法

        SqlSession sqlSession = sqlSessionFactory.openSession();
        Brandmapper mapper = sqlSession.getMapper(Brandmapper.class);
        List<Brand> brands = mapper.selectAll();
        sqlSession.close();
        return brands;


    }
    public void insert(Brand brand)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Brandmapper mapper = sqlSession.getMapper(Brandmapper.class);
        mapper.insertBrand(brand);
        sqlSession.commit();
        sqlSession.close();

    }
    public void updateBrand(Brand brand)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Brandmapper mapper = sqlSession.getMapper(Brandmapper.class);
        mapper.update(brand);
        sqlSession.commit();
        sqlSession.close();
    }
    public Brand selectById(Integer id)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        Brandmapper mapper = sqlSession.getMapper(Brandmapper.class);
       Brand brand =  mapper.selectById(id);

        sqlSession.close();
        return brand;
    }
}
